package com.shanghaionstar.user.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.more.response.VehicleImgRes;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;

import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/8/31.
 */
public class ChooseVehicleImgAdapter extends com.onstar.cn.framework.base.CommonAdapter<VehicleImgRes> {
    private Context context;
    private List<VehicleImgRes> list;
    private ImageLoader imageLoader;


    public ChooseVehicleImgAdapter(List<VehicleImgRes> datas, Context context,ImageLoader imageLoader) {
        super(datas, context);
        this.context=context;
        this.list=datas;
        this.imageLoader=imageLoader;
    }

    @Override
    public void getView(int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        if(list==null){
            return;
        }
        VehicleImgRes vehicleImgRes = list.get(position);
        String modelDesc = vehicleImgRes.getModelDesc();
        String imageURL = vehicleImgRes.getImageURL();
        String thumbnailURL = vehicleImgRes.getThumbnailURL();
        holder.modle_tv.setText(modelDesc == null ? "" : modelDesc);
        DisplayImageOptions options = OnstarApplication
                .getOptions(R.drawable.onstar_vehicle_img_loading);
        System.gc();
        imageLoader.displayImage(thumbnailURL, holder.vehicle_img_iv, options,
                new AnimateFirstDisplayListener());
        if (position % 2 == 0) {
            holder.right_line_v.setVisibility(View.VISIBLE);
        } else {
            holder.right_line_v.setVisibility(View.GONE);
        }
    }

    @Override
    public int layoutId() {
        return R.layout.choose_car_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder{
        @FindViewById
        private CustomImageView vehicle_img_iv;
        @FindViewById
        private TextView        modle_tv;
        @FindViewById
        private View            right_line_v;
    }

    /**
     * 图片加载第一次显示监听器
     *
     * @author Administrator
     *
     */
    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                // 是否第一次显示
                FadeInBitmapDisplayer.animate(imageView, 500);
            }
        }
    }

}
